<template>
  <!-- 业务组件配置管理 -->
  <BaseMainSlot :show-tab="true">
    <template v-slot:btn-box>
      <ActionBar :add-active="addActive" :del-active="delActive" :cancel-active="cancelActive" :has-edit="false"
        :has-del="false" :has-save="false" :save-active="saveActive" @add="add" @cancel="cancel" @del="del" />
    </template>
    <template v-slot:tab-select>
      <el-upload ref="upload" class="upload-demo" action="doUpload" :limit="1" :before-upload="beforeUpload">
        <el-button slot="trigger" type="text" class="DownExcel" icon="el-icon-upload2" :loading="upLoading" size="mini"
          style="margin-right: 20px">上传敏感词表</el-button>
      </el-upload>
    </template>
    <template v-slot:search-box>
      <SearchBox @search="search">
        <div class="common-search-item">
          <div class="label">敏感词类型:</div>
          <div class="input">
            <el-select v-model="type" placeholder="选择敏感词类型" @change="search">
              <el-option v-for="(item, index) in typeList" :key="index" :value="item" :label="item" />
            </el-select>
          </div>
        </div>
        <div class="common-search-item">
          <div class="label">敏感词:</div>
          <div class="input">
            <el-input v-model="words" placeholder="请输入敏感词" clearable @input="search()" />
          </div>
        </div>
        <!-- <div class="common-search-item">
          <div class="label">承租户:</div>
          <div class="input">
            <el-input
              v-model="lessee"
              placeholder="请输入承租户名称"
              clearable
            />
          </div>
        </div> -->
      </SearchBox>
    </template>
    <template v-slot:table-box>
      <!-- 业务组件展示表格 -->
      <div class="zh-table-view">
        <el-table :data="tableData" style="width: 100%" @row-click="handleEdit" height="100%">
          <el-table-column type="index" width="50" />
          <el-table-column prop="type" label="类型" />
          <el-table-column prop="words" label="敏感词" />
          <el-table-column label="状态">
            <template slot-scope="scope">
              <el-switch v-model="scope.row.status" active-color="#13ce66" inactive-color="#ff4949"
                style="margin-left: 10px" :inactive-value="false" :active-value="true"
                @change="savemesedit($event, scope.row)" />
            </template>
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button size="mini" type="primary" @click="edit">修改</el-button>
              <el-button size="mini" type="warning" @click="dellineallmes(scope.$index, scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <Pagination :page-size="pageSize" :current-page="currentPage" :total-size="totalSize"
          @changePageSize="changePageSize" @changeCurrentPage="changeCurrentPage" />
      </div>
    </template>

    <template v-slot:dialog>
      <!-- 新增业务组件的弹框 -->
      <el-dialog :title="titleText + '敏感词'" class="formData" :visible.sync="dialogFormVisible" width="50%" top="5vh"
        :before-close="() => { dialogFormVisible = false }">
        <el-form ref="formData" label-position="left" :model="formData" :rules="rules" class="el-form">
          <el-form-item label="敏感词类型:" prop="type" class="w100p">
            <!-- <el-input v-model="formData.type" :disabled="nowChanceStau === 'edit'" clearable /> -->
            <el-select v-model="formData.type" placeholder="选择敏感词类型">
              <el-option v-for="(item, index) in typeList" :key="index" :value="item" :label="item" />
            </el-select>
          </el-form-item>
          <el-form-item label="敏感词:" prop="words" class="w100p">
            <el-input v-model="formData.words" clearable />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="cancel()">取 消</el-button>
          <el-button type="primary" @click="savemes">保 存</el-button>
        </div>
      </el-dialog>
    </template>
  </BaseMainSlot>
</template>
<script>
import ActionBar from "@/components/ActionBar/index";
import { mymixin } from "@/utils/mixins";
import SearchBox from "@/components/SearchBox";
import BaseMainSlot from "@/components/BaseMainSlot/newBaseMainSlot.vue";
import zhreq from '@/api/system-manage/sensitive-word-manage'
import Pagination from "@/components/Pagination";
import axios from "axios";
// import { getTitleList } from "@/components/getnameurl";
export default {
  components: { ActionBar, Pagination, BaseMainSlot, SearchBox },
  mixins: [mymixin],
  data() {
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.formData.newPassword) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      isshowuploadpic: false,
      tableData: [],
      pageSize: 10,
      currentPage: 1,
      totalSize: 0,
      // ---添加相关---
      diastau: 0,
      upLoading: false,
      formData: {
      },
      selectStau: '',
      typeList: ["宪法相关", "国家安全", "民族团结", "邪教封建", "社会次序", "色情暴力", "国家领导人", "罢工上访", "司法纪检"],
      nickName: undefined,
      type: '',
      words: "",
      omUserName: undefined,
      unitList: [],
      image: null,
      files: null,
      unitLists: [],
      databaseName: 'DB_TDW1_PUBLIC',
      plantVarietyPictureNo: '',
      rules: {
        omUserName: [{ required: true, message: "请输入账号", trigger: "blur" }],
        nickName: [{ required: true, message: "请输入昵称", trigger: "blur" }],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 6,
            max: 16,
            message: "长度在 6 到 16 个字符",
            trigger: "blur"
          }
        ],
        newPassword: [
          { required: true, message: "请输入新密码", trigger: "blur" },
          {
            min: 6,
            max: 16,
            message: "长度在 6 到 16 个字符",
            trigger: "blur"
          }
        ],
        newPasswords: [
          { required: true, message: "请再次输入密码", trigger: "blur" },
          {
            validator: validatePass2,
            trigger: "blur"
          }
        ]
      }
    }
  },
  created() {
    this.queryDataList()
  },
  mounted() {
    this.gettabledataList()
  },
  methods: {
    beforeUpload(file) {
      // console.log(file,'文件');
      this.files = file;
      // const extension = file.name.split('.')[1] === 'xls'
      // const extension2 = file.name.split('.')[1] === 'xlsx'
      // const isLt5M = file.size / 1024 / 1024 < 5
      // if (!extension && !extension2) {
      //   this.$message.warning('上传模板只能是 xls、xlsx格式!')
      //   return
      // }
      // if (!isLt5M) {
      //   this.$message.warning('上传模板大小不能超过 5MB!')
      //   return
      // }
      this.fileName = this.files.name;
      setTimeout(() => {
        this.submitUpload();
        this.upLoading = true
      }, 500);
      return false; // 返回false不会自动上传
    },
    // 上传excel
    async submitUpload() {
      if (this.fileName === "") {
        this.$message.warning('请选择要上传的文件！')
        return false
      }
      let fileFormData = new FormData();
      // fileFormData.append("code", "t_pathology_info_excel");
      // fileFormData.append("description", "excel上传测试");
      // filename是键，file是值，就是要传的文件
      fileFormData.append('multipartFile', this.files, this.fileName);
      // 执行上传excel
      // console.log(Promise.resolve(basicInfoManage.employeeImportExcel(fileFormData)))
      // const { data: res } = await
      // basicInfoManage.employeeImportExcel(fileFormData).then(resp => {
      //   if (resp.code === 1) {
      //     this.$message.success(resp.msg || "上传成功！");
      //     this.upLoading = false
      //     this.getTableDataList()
      //   } else {
      //     this.$message.error(resp.msg || "上传失败！");
      //   }
      // })
      const { data: mes } = await axios({
        method: "post",
        url: this.$webSerUrlPro + 'config/sensitiveWords/inputByFile',
        headers: {
          databaseName: this.databaseName,
          responseType: 'blob'
        },
        data: fileFormData
      })
      if (mes.code === 1) {
        this.$message.success(mes.msg || "上传成功！");
        this.upLoading = false
        this.gettabledataList()
      } else {
        this.$message.error(mes.msg || "上传失败！");
      }
    },
    // 获取下拉框信息
    async queryDataList() {
      // 获取昵称
      const { data: res } = await zhreq.queryByCondition({})
      this.unitTypeList = res
      // console.log(res)
      // console.log(this.unitTypeList)
    },
    // 获取表格信息
    gettabledataList() {
      let params = {
        page: this.currentPage,
        size: this.pageSize,
        entity: {
          type: this.type || undefined,
          words: this.words || undefined
        }
      }
      zhreq.getList(params).then(response => {
        let mes = response.data
        if (response.code === 1) {
          this.tableData = mes.content || []
          this.totalSize = mes.totalElements
          this.resetActionBar()
        } else {
          this.$message.warning("获取表格失败")
        }
      })
    },
    // 搜索
    search() {
      this.currentPage = 1;
      this.gettabledataList();
    },
    changePageSize(pageSize) {
      this.pageSize = pageSize;
      this.gettabledataList();
    },
    changeCurrentPage(currentPage) {
      this.currentPage = currentPage;
      this.gettabledataList();
    },

    // // 上传图片
    ImageUrl() {
      // console.log(this.nowChanceStau)
      if (this.nowChanceStau === "edit" || this.nowChanceStau === "add") {
        let URL = `${this.$webSerUrlPro}category/plantVarietyPicture/add`;
        // console.log(URL)
        return URL;
      } else {
        let URL = "#";
        // console.log(URL)
        return URL;
      }
    },
    handleResponse(response, file, fileList) {
      if (this.nowChanceStau === "edit" || this.nowChanceStau === "add") {
        // return this.basicUrl + query/ + this.plantVarietyPictureNo
        // console.log(response)
        this.plantVarietyPictureNo = response.data.plantVarietyPictureNo;
        this.$set(this.formData, "pictureNo", response.data.plantVarietyPictureNo);
        zhreq.getColectPicture(this.plantVarietyPictureNo).then(
          (res) => {
            this.image = "data:image/png;base64," + res.data.pictureData;
          }
        );
        return this.image;
      } else {
        return this.$message.error("点击修改按钮再进行编辑!");
      }
    },
    handleRemove() {
      if (this.nowChanceStau === "edit" || this.nowChanceStau === "add") {
        // console.log(response);
        zhreq.delCollectionPicture(this.plantVarietyPictureNo).then((res) => {
          if (res.code === 1) {
            delete this.plantVarietyPictureNo;
            delete this.formData.plantVarietyPictureNo;
            this.$message.success(res.msg);
          }
        });
      } else {
        return this.$message.error("点击修改按钮再进行编辑!");
      }
    },
    // 删除一个业务组件
    dellineallmes(i, e) {
      console.log(e)
      // return
      this.$confirm('此操作将删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        zhreq.delList(e.sensitiveWordsNo).then(
          res => {
            if (res.code === 1) {
              this.$message.success("删除成功！")
              this.gettabledataList()
            }
          }
        )
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      })
    },
    handleEdit(row) {
      if (row) {
        this.formData = JSON.parse(JSON.stringify(row))
        // zhreq.getColectPicture(this.formData.pictureNo).then(
        //   (res) => {
        //     this.image = "data:image/png;base64," + res.data.pictureData;
        //   }
        // );
      }
    },
    // 修改一个业务组件
    editlineallmes(i, e) {
      if (e) {
        this.formData = JSON.parse(JSON.stringify(e))
        this.diastau = 3
      }
    },
    // 保存要添加的业务的组件信息
    savemes() {
      this.$refs['formData'].validate(async (valid) => {
        if (valid) {
          if (this.nowChanceStau === "edit") {
            let oldPassword = ''
            if (this.formData.oldPassword) {
              oldPassword = this.formData.oldPassword
            }
            let param = {
              ...this.formData,
              password: oldPassword || undefined
            }
            zhreq.editList(param).then(response => {
              let mes = response
              if (mes.code === 1) {
                this.diastau = 0
                this.$message.success("修改成功！")
                this.gettabledataList()
              }
            })
          } else if (this.nowChanceStau === "add") {
            let param = {
              ...this.formData,
              expire: true
            }
            zhreq.addList(param).then(response => {
              let mes = response
              if (mes.code === 1) {
                this.$message.success("添加成功！")
                this.gettabledataList()
              }
            })
          }
        } else {
          // console.log('error submit!!');
          return false;
        }
      });
    },
    // 修改时-保存要添加的业务的组件信息
    savemesedit(e, v) {
      let param = {
        ...v
      }
      zhreq.editList(param).then(response => {
        let mes = response
        if (mes.code === 1) {
          this.diastau = 0
          this.$message.success("修改成功！")
          this.gettabledataList()
        }
      })
    },
    // 获取状态
    getstau(e) {
      this.selectStau = e
      if (e === 'add') {
        this.diastau = 1
      } else if (e === 'complete') {
        this.diastau = 0
      } else if (e === 'edit') {
        this.diastau = 3
      }
    },
    // 设置弹框展示
    setdiaview(i) {
      this.diastau = i
    }
  }
}
</script>

<style lang="scss" scoped>
@import "~@/styles/variables.scss";

.business-main {
  width: 100%;
  height: 100%;

  .topbar-view {
    width: 100%;
    margin-bottom: 5px;
  }

  .zh-table-view {
    width: 100%;
    height: calc(100% - 60px);
    position: relative;
  }
}

.add-components-dia {
  width: 502px;
  // height: 191px;
  background: #303640;
  border: 1px solid #005c6f;
  border-radius: 7px;
  position: fixed;
  top: 15%;
  left: 40%;
  // background-color: #005c6f;
  z-index: 99;

  .top-bar {
    width: 502px;
    height: 10px;
    background: linear-gradient(90deg, #3581fb 0%, #00faad 100%);
    border-radius: 7px 7px 0px 0px;
  }

  .top-title-line {
    width: 490px;
    height: 35px;
    line-height: 35px;
    padding: 6px 8px;
    display: flex;
    justify-content: space-between;
    border-bottom: 1px solid #484a4e;

    .line-title {
      height: 20px;
      font-size: 14px;
      font-family: PingFang SC-Regular, PingFang SC;
      font-weight: 400;
      color: $textColor;
      letter-spacing: 2px;
    }

    .line-close {
      color: #10d7c4;
      cursor: pointer;
    }

    .line-close:hover {
      color: $textColor;
    }
  }

  .screen-dia-main {
    width: 502px;
    padding: 20px 10px;
    // max-height: 550px;
    overflow-y: auto;
    overflow-x: hidden;
    max-height: 480px;

    .title-config {
      padding-bottom: 20px;
      border-bottom: #00faad 1px solid;
      margin-bottom: 10px;
    }

    .dia-main-inputline {
      width: 482px;
      margin-top: 5px;
      display: flex;
      // justify-content: center;
      align-items: center;
      margin-bottom: 10px;

      .inputline-name {
        width: 120px;
        height: 24px;
        font-size: 16px;
        font-family: PingFang SC-Regular, PingFang SC;
        font-weight: 400;
        color: $textColor;
        line-height: 24px;
        text-align: center;
      }

      .inputline-value {
        width: 350px;
        height: 32px;
        box-sizing: border-box;
        border-radius: 0px 0px 0px 0px;
        opacity: 1;
        border: 1px solid #494e57;
        box-sizing: border-box;
        outline: 0;
      }

      .textarealine-value {
        width: 350px;
        height: 32px;
        box-sizing: border-box;
        border-radius: 0px 0px 0px 0px;
        opacity: 1;
        border: 1px solid #494e57;
        box-sizing: border-box;
        outline: 0;
      }

      .icon-line-value {
        width: 48px;
        height: 48px;
        text-align: center;

        .not-iconurl {
          border: 1px dashed #d9d9d9;
          border-radius: 6px;
          cursor: pointer;
          position: relative;
          overflow: hidden;
        }

        .not-iconurl:hover {
          border-color: #1caf7e;
        }

        .not-iconsty {
          font-size: 20px;
          color: #8c939d;
          width: 48px;
          height: 48px;
          line-height: 48px;
          text-align: center;
        }
      }

      .selcect-one {
        width: 350px;
        height: 32px;
        box-sizing: border-box;
        border-radius: 0px 0px 0px 0px;
        opacity: 1;
        border: 1px solid #494e57;
        box-sizing: border-box;
        outline: 0;
      }
    }

    .dia-main-addline {
      width: 442px;
      font-size: 16px;
      font-family: PingFang SC-Regular, PingFang SC;
      font-weight: 400;
      color: $textColor;
      line-height: 24px;
      border-bottom: #00faad 1px dashed;
      padding-bottom: 16px;
      margin-bottom: 10px;

      .components-line {
        display: flex;
        // justify-content: space-between;
        align-items: center;
        width: 442px;
        height: 48px;

        .slect-name {
          width: 90px;
          height: 36px;
          font-size: 16px;
          font-family: PingFang SC-Regular, PingFang SC;
          font-weight: 400;
          color: $textColor;
          line-height: 36px;
          text-align: center;
        }

        .selcect-one {
          width: 350px;
          color: #00faad;
          margin-top: 12px;
          height: 36px;
          line-height: 36px;
          background-color: transparent;
          border: #00faad dashed 1px;
          cursor: pointer;

          option {
            background-color: #005c6f !important;
            color: $textColor;
            cursor: pointer;
          }
        }
      }

      .addline-but {
        width: 442px;
        display: flex;
        justify-content: space-around;
        margin-top: 15px;

        .addline-but1 {
          color: #00faad;
          border: 1px solid #00faad;
          cursor: pointer;
          padding: 5px 10px;
        }

        .addline-but1:hover {
          color: $textColor;
        }

        .addline-but2 {
          color: #3581fb;
          border: 1px solid #3581fb;
          cursor: pointer;
          padding: 5px 10px;
        }

        .addline-but2:hover {
          color: $textColor;
        }
      }
    }

    .dia-main-addline:last-of-type {
      padding-bottom: 0;
      margin: 0;
      border: none;
    }

    .showkuang-one {
      position: relative;
      left: 41px;
      width: 400px;
      min-height: 200px;

      .kuangone-main {
        position: absolute;
        width: 100%;
        height: 400px;
      }
    }
  }

  .bottom-bar {
    width: 482px;
    height: 50px;
    display: flex;
    justify-content: space-around;
    align-items: center;
    font-size: 16px;

    .bottom-bar-cancel {
      width: 96px;
      height: 33px;
      border-radius: 5px 5px 5px 5px;
      border: 1px solid #0cb9ae;
      color: #0cb9ae;
      cursor: pointer;
      line-height: 33px;
      text-align: center;
    }

    .bottom-bar-cancel:hover {
      color: #00faad;
    }

    .bottom-bar-confirm {
      width: 96px;
      height: 33px;
      background: #0cb9ae;
      border-radius: 5px 5px 5px 5px;
      cursor: pointer;
      line-height: 33px;
      text-align: center;
    }

    .bottom-bar-confirm:hover {
      color: #10fab4;
    }
  }
}

::v-deep .el-upload--picture-card {
  width: 40px;
  height: 40px;
  background-color: rgba(255, 255, 255, 0.1);
  border: 1px solid #6ecae3;
}

::v-deep .el-upload--picture-card {
  position: relative;
}

::v-deep .el-upload--picture-card .el-icon-plus {
  font-size: 34px;
  color: #6ecae3;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

::v-deep .el-dialog__body {
  height: 90%;
  text-align: center;

  img {
    width: 100%;
  }
}

::v-deep .el-upload__tip {
  display: none;
}
</style>
